package com.bloody.android;

import java.util.ArrayList;

import com.bloody.android.Map.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import com.bloody.android.Characters.Character;
import com.bloody.android.Characters.ViewCharacterInfo;

public class BloodyCrusadeView extends View {
	
	protected static int mTileSize;
    private final Paint mPaint = new Paint();
    private int mXOffset;
    private int mYOffset;
    private int cornerx=0;
    private int cornery=0;
    private Context context;
    private int sizeView = 5;
    private ArrayList<ViewObject> vElement;
    private BloodyCrusadeGame bcg;
    
    private static final String TAG = "BloodyView";

	public BloodyCrusadeView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context= context;
		vElement = new ArrayList<ViewObject>();
	}
	
	public void setCornery(int cornery) {
		this.cornery = cornery;
	}
	
	public void setBcg(BloodyCrusadeGame bcg) {
		this.bcg = bcg;
	}

	public void setCornerx(int cornerx) {
		this.cornerx = cornerx;
	}
	
	public void addViewObject(ViewObject vObject){
		vElement.add(vObject);
		vElement.get(vElement.size()-1).createIm(mTileSize);
	}
	
	public int[] positionMap(float x, float y){
		int[] posMap = new int[2];
		posMap[0]= (int)(x-mXOffset)/mTileSize;
		posMap[1]= (int)(y-mYOffset)/mTileSize;
		return posMap;
	}
	
	private void createViewMap(Map map){
		map.setViewCase(context, vElement);
	}
	
	private void createViewCharacterInfo(ArrayList<Character> ch){
		vElement.add(new ViewCharacterInfo(context, mYOffset, ch));	
	}
	
	public void createViewElement(Map map, ArrayList<Character> ch){
		createViewMap(map);
		createViewCharacterInfo(ch);
		for (int i=0; i<vElement.size(); i++){
        	vElement.get(i).createIm(mTileSize);
        }	
        setFocusable(true);
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh){
		if (w<=h)
			mTileSize = (int) Math.floor(w / this.sizeView);
		else
			mTileSize = (int) Math.floor(h / this.sizeView);

        mXOffset = ((w - (mTileSize * this.sizeView)) / 2);
        mYOffset = ((h - (mTileSize * this.sizeView)) / 2);	
        bcg.beginGame();
	}
	
	@Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i=0; i<vElement.size(); i++)
        	vElement.get(i).draw(canvas, mXOffset, mYOffset, mTileSize, mPaint, cornerx, cornerx+sizeView-1, cornery, cornery + sizeView -1);
   		
    }
}
